package com.xiaobao.controller;

import com.xiaobao.pojo.Result;
import com.xiaobao.pojo.User;
import com.xiaobao.service.UserService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

//用户控制器
@RestController
public class UserController {
    private static final Logger log = LogManager.getLogger(UserController.class);
    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    //修改用户
    @PostMapping("/updateUser")
    public Result<User> updateUser(@RequestBody User user){
        log.info("修改用户信息");
        log.info(user);
        if(userService.updateUser(user)){
            return new Result<>(true,200,"修改成功",user);
        }
        return new Result<>(false,400,"修改失败",user);
    }
    //添加用户
    @PostMapping("/addUser")
    public Result<User> addUser(@RequestBody User user){
        log.info("添加用户");
        log.info(user);
        if(userService.addUser(user)){
            return new Result<>(true,200,"添加成功",user);
        }
        return new Result<>(false,400,"添加失败",user);
    }
    //删除用户信息
    //当删除这个用户时所有的考研信息也删除，等等都要删除
    @GetMapping("/deleteUser")
    public Result<User> deleteUser(String id){
        log.info("删除用户");
        log.info(id);
        if(userService.deleteUser(id)){
            return new Result<>(true,200,"删除成功",null);
        }
        return new Result<>(false,400,"删除失败",null);
    }
}
